<template>
  <div>
    <el-card>
      <!--搜索框11-->
      <el-row :gutter="20">
        <el-col :span="4">
          <el-input placeholder="请输入长者姓名" clearable v-model="name" @clear="search">
            <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="openAddGluApplication">创建血糖</el-button>
        </el-col>
      </el-row>
      <!--添加血糖-->
      <el-dialog title="添加血糖" :visible.sync="addGluFormVisible" @close="closeAddGluForm"
                 :close-on-click-modal="false">
        <el-dialog
            width="75%"
            title="选择长者信息"
            :visible.sync="innerVisible"
            append-to-body>
          <el-input placeholder="请输入长者姓名" clearable v-model="olderName" @clear="searchOlder" style="width: 250px">
            <el-button slot="append" icon="el-icon-search" @click="searchOlder"></el-button>
          </el-input>

          <template>
            <el-table
                :data="olderList"
                style="width: 100%"
                :row-class-name="tableRowClassName"
            >
              <el-table-column
                  label="编号"
                  type="index"
                  width="100">
              </el-table-column>
              <el-table-column
                  prop="name"
                  label="长者姓名"
                  width="100">
              </el-table-column>
              <el-table-column
                  prop="sex"
                  label="性别"
                  width="100">
              </el-table-column>
              <el-table-column
                  prop="bed.bedname"
                  label="床位号"
                  width="300">
              </el-table-column>
              <el-table-column
                  prop="idcard"
                  label="长者身份证号"
                  width="220">
              </el-table-column>
              <el-table-column
                  prop="checkdate"
                  label="入住日期"
                  width="150">
              </el-table-column>
              <el-table-column
                  label="在院状态"
                  width="120"
              >
                <template slot-scope="scope">
                  <span style="color: red" v-if="scope.row.status == 0">退房</span>
                  <span style="color: green" v-if="scope.row.status == 1">入住</span>
                  <span style="color: red" v-if="scope.row.status == 2">待退房</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="150">
                <template slot-scope="scope">
                  <el-button
                      size="mini"
                      v-if="scope.row.status != 0"
                      @click="selectOlderAdd(scope.row.id,scope.row.name)">选择
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </template>
          <el-pagination
              @size-change="olderSizeChange"
              @current-change="olderCurrentChange"
              :current-page="olderPageNum"
              :page-sizes="[3, 6 , 12, 24]"
              :page-size="olderPageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="olderTotal"
          ></el-pagination>
        </el-dialog>
        <el-form ref="addGluForm" :rules="addGluFormRules" :model="addGluForm" label-width="150px" size="big">
          <el-form-item label="老者" prop="name" @click="innerVisible = true">
            <el-button type="primary" v-model="addGluForm.olderId" @click="innerVisible = true" style="width: 100px">
              {{ addGluForm.name }}
            </el-button>
          </el-form-item>


          <el-form-item label="采集日期" prop="collectDate">
            <el-date-picker
                v-model="addGluForm.collectDate"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                :editable="false"
                type="date"
                placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="数值" prop="num">
            <el-input v-model="addGluForm.num" placeholder="单位：mmol/L" style="width: 220px"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeAddGluForm">取 消</el-button>
          <el-button type="primary" @click="AddGluFormOK">确 定</el-button>
        </div>
      </el-dialog>
      <!--主体数据-->
      <template>
        <el-table
            :data="GluApplicationList"
            style="width: 100%"
            :row-class-name="tableRowClassName"

        >
          <el-table-column
              label="编号"
              type="index"
              width="200">
          </el-table-column>
          <el-table-column
              prop="name"
              label="长者姓名"
              width="250">
          </el-table-column>
          <el-table-column
              prop="idcard"
              label="长者身份证号"
              width="350">
          </el-table-column>
          <el-table-column
              prop="collectDate"
              label="采集日期"
              width="250">
          </el-table-column>
          <el-table-column
              prop="num"
              label="血糖/mmol/L"
              width="190">
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  @click="handleEdit(scope.row.id)">编辑
              </el-button>
              <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row.id)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!--分页-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[3, 6 , 12, 24]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
      <!--编辑血糖-->
      <el-dialog title="编辑血糖" :visible.sync="modifyGluFormVisible" @close="closeModifyGluForm"
                 :close-on-click-modal="false">
        <el-dialog
            width="75%"
            title="选择长者信息"
            :visible.sync="innerVisible"
            append-to-body>
          <el-input placeholder="请输入长者姓名" clearable v-model="olderName" @clear="searchOlder">
            <el-button slot="append" icon="el-icon-search" @click="searchOlder"></el-button>
          </el-input>
          <el-pagination
              @size-change="olderSizeChange"
              @current-change="olderCurrentChange"
              :current-page="olderPageNum"
              :page-sizes="[3, 6 , 12, 24]"
              :page-size="olderPageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="olderTotal"
          ></el-pagination>
          <template>
            <el-table
                :data="olderList"
                style="width: 100%"
                :row-class-name="tableRowClassName"
            >
              <el-table-column
                  label="编号"
                  type="index"
                  width="60">
              </el-table-column>
              <el-table-column
                  prop="name"
                  label="长者姓名"
                  width="100">
              </el-table-column>
              <el-table-column
                  prop="sex"
                  label="性别"
                  width="100">
              </el-table-column>
              <el-table-column
                  prop="bed.bedname"
                  label="床位号"
                  width="100">
              </el-table-column>
              <el-table-column
                  prop="idcard"
                  label="身份证号"
                  width="220">
              </el-table-column>
              <el-table-column
                  prop="checkdate"
                  label="入住日期"
                  width="150">
              </el-table-column>
              <el-table-column
                  label="在院状态"
                  width="120"
              >
                <template slot-scope="scope">
                  <span style="color: red" v-if="scope.row.status == 0">退房</span>
                  <span style="color: green" v-if="scope.row.status == 1">入住</span>
                  <span style="color: red" v-if="scope.row.status == 2">待退房</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="240">
                <template slot-scope="scope">
                  <el-button
                      size="mini"
                      v-if="scope.row.status != 0"
                      @click="selectOlder(scope.row.id,scope.row.name)">选择
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-dialog>
        <el-form ref="modifyGluForm" :rules="modifyGluFormRules" :model="modifyGluForm" label-width="150px" size="big">
          <el-form-item label="长者" prop="name" @click="innerVisible = true">
            <el-button type="primary" v-model="modifyGluForm.olderId" @click="innerVisible = true" style="width: 100px">
              {{ modifyGluForm.name }}
            </el-button>
          </el-form-item>
          <el-form-item label="采集日期" prop="collectDate">
            <el-date-picker
                v-model="modifyGluForm.collectDate"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                :editable="false"
                type="date"
                placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="数值/mmol/L" prop="num">
            <el-input v-model="modifyGluForm.num" style="width: 220px"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeModifyGluForm">取 消</el-button>
          <el-button type="primary" @click="modifyGluFormOK">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {toRefs} from "vue";

export default {
  data() {
    return {
      olderName: '',
      olderList: [],
      innerVisible: false,
      addGluForm: {
        olderId: 0,
        num: '',
        collectDate: '',
        name: '',
      },
      modifyGluForm: {
        olderId: 0,
        num: '',
        collectDate: '',
        name: '',
      },
      GluApplicationList: [],
      modifyGluFormVisible: false,
      addGluFormVisible: false,
      name: '',
      pageNum: 1,
      olderPageNum: 1,
      pageSize: 3,
      olderPageSize: 3,
      total: 0,
      olderTotal: 0,
      addGluFormRules: {
        name: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
        num: [
          {required: true, message: '不能为空', trigger: 'blur'},
          {pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/, message: '必须为大于0的数字', trigger: 'blur'}
        ],
        collectDate: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
      },
      modifyGluFormRules: {
        num: [
          {required: true, message: '不能为空', trigger: 'blur'},
          {pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/, message: '必须为大于0的数字', trigger: 'blur'}
        ],
        collectDate: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
      },
    };
  },
  methods: {
    tableRowClassName({rowIndex}) {
      if (rowIndex === 0) {
        return 'white-row'; // 第一行为白色
      } else if (rowIndex % 4 === 1) {
        return 'warning-row'; // 第二行和每隔三行后的行为绿色
      } else if (rowIndex % 2 === 0) {
        return ''; // 第二行和每隔三行后的行为绿色
      } else if (rowIndex % 3 === 2) {
        return 'white-row'; // 第三行和每隔三行后的行为白色
      } else {
        return 'success-row'; // 每隔三行后的行为黄色
      }
    },

    handleDelete(id) {
      this.$confirm("你确定要删除此记录吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let url = '/glu/delete/'+id;
        this.$axios.delete(url).then((res) => {
          if (res.data.code == 200) {
            this.$message({
              message: res.data.msg,
              type: 'success',
              duration: 1000
            });
            this.initGluApplicationList();
          } else {
            this.$message({
              message: res.data.msg,
              type: 'error',
              duration: 1000
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: "info",
          message: "取消成功"
        })
      })

    },
    openAddGluApplication(id) {
      this.addGluFormVisible = true;
      this.addGluForm = {};
    },
    closeAddGluForm() {
      this.addGluFormVisible = false;
    },
    //选择长者
    selectOlder(id, name) {

      this.modifyGluForm.olderId = id;

      this.modifyGluForm.name = name;

      this.innerVisible = false;
    },
    selectOlderAdd(id, name) {

      this.addGluForm.olderId = id;

      this.addGluForm.name = name;

      this.innerVisible = false;
    },
    //修改申请表
    handleEdit(id) {
      this.modifyGluFormVisible = true;
      let url = "/glu/one/" + id;
      this.$axios.get(url).then((res) => {
        if (res.data.code == 200) {
          this.modifyGluForm = res.data.data;
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error',
            duration: 1000
          });
        }
      })
    },
    //确认添加
    AddGluFormOK() {
      this.$refs.addGluForm.validate((vaild) => {
        if (!vaild) {
          return
        } else {
          this.$confirm("你确定要创建吗？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            let url = '/glu/add'
            this.$axios.post(url, this.addGluForm).then((res) => {
              if (res.data.code == 200) {
                this.$message({
                  message: res.data.msg,
                  type: 'success',
                  duration: 1000
                });
                this.initGluApplicationList();
                this.addGluFormVisible = false;
              } else {
                this.$message({
                  message: res.data.msg,
                  type: 'error',
                  duration: 1000
                });
              }
            })
          }).catch(() => {
            this.$message({
              type: "info",
              message: "取消成功"
            })
          })
        }
      })
    },
    //确认修改
    modifyGluFormOK() {
      this.$refs.modifyGluForm.validate((vaild) => {
        if (!vaild) {
          return
        } else {
          let url = '/glu/modify'
          this.$axios.put(url, this.modifyGluForm).then((res) => {
            if (res.data.code == 200) {
              this.$message({
                message: res.data.msg,
                type: 'success',
                duration: 1000
              });
              this.initGluApplicationList();
              this.modifyGluFormVisible = false;
            } else {
              this.$message({
                message: res.data.msg,
                type: 'error',
                duration: 1000
              });
            }
          })
        }
      })
    },
    //关闭修改对话框
    closeModifyGluForm() {
      this.modifyGluFormVisible = false;
    },
    //查询长者
    search() {
      this.pageNum = 1;
      this.initGluApplicationList();
    },
    //长者内置搜收框
    searchOlder() {
      this.olderPageNum = 1;
      this.initOlderList();
    },
    //初始化长者信息
    initOlderList() {
      let url = "/Older/list"
      this.$axios.get(url, {
        params: {
          name: this.olderName,
          now: this.olderPageNum,
          size: this.olderPageSize
        }
      }).then((res) => {
        if (res.data.code == 200) {
          this.olderList = res.data.data.list;
          this.olderPageNum = res.data.data.pageNum;
          this.olderPageSize = res.data.data.pageSize;
          this.olderTotal = res.data.data.total;
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error',
            duration: 1000
          });
        }
      })
    },
    //初始化查询外出申请数据
    initGluApplicationList() {
      let url = "/glu/list"
      this.$axios.get(url, {
        params: {
          name: this.name,
          now: this.pageNum,
          size: this.pageSize
        }
      }).then((res) => {
        if (res.data.code == 200) {
          this.GluApplicationList = res.data.data.list;
          this.pageNum = res.data.data.pageNum;
          this.pageSize = res.data.data.pageSize;
          this.total = res.data.data.total;
        } else {
          this.$message({
            message: res.data.msg,
            type: 'error',
            duration: 1000
          });
        }
      })
    },
    //改变条数查询
    handleSizeChange(size) {
      this.pageSize = size;
      this.pageNum = 1;
      this.initGluApplicationList();
    },
    //改变长者条数查询
    olderSizeChange(size) {
      this.olderPageSize = size;
      this.olderPageNum = 1;
      this.initOlderList();
    },
    //改变页数查询
    handleCurrentChange(now) {
      this.pageNum = now;
      this.initGluApplicationList();
    },
    //改变长者页数查询
    olderCurrentChange(now) {
      this.olderPageNum = now;
      this.initOlderList();
    },
  },
  created() {
    this.initGluApplicationList();
    this.initOlderList();
  }
}
</script>

<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

</style>